<?php
/**
 * Created by PhpStorm.
 * User: kkw
 * Date: 2019/1/27
 * Time: 13:05
 */

namespace kkw\user\response;

use kkw\user\table\UserTable;
use kkw\user\Util;
use suda\core\Request;
use suda\core\Response;

class RegisterResponse extends  Response
{
    public function onRequest(Request $request)
    {
        Util::printHeader();
        $user = new UserTable();
        $json =  Request::json();
        try {
            $user->insert([
                'name' => $json['name'],
                'password' => md5($json['pwd']),
                'mobile' => $json['phone']
            ]);
        } catch (\suda\exception\SQLException $e) {
            $this->json(['code' => $e->getCode(), 'message' => '注册失败,换个手机号试试吧']);
            return;
        }
        $this->json(['code' => 0, 'message' => '注册成功']);
    }
}